<?php

/**
 * This is the model class for table "tbl_ask_reply".
 *
 * The followings are the available columns in table 'tbl_ask_reply':
 * @property integer $reply_id
 * @property integer $ask_id
 * @property string $body
 * @property integer $user_id
 * @property string $createdOn
 * @property integer $isFloor
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Ask $ask
 */
class AskReply extends CActiveRecord
{
    
                    public $verifyCode;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_ask_reply';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('body, verifyCode', 'required'),
			array('ask_id, user_id, isFloor, status', 'numerical', 'integerOnly'=>true),
			array('body, createdOn', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('reply_id, ask_id, body, user_id, createdOn, isFloor, status', 'safe', 'on'=>'search'),
                                                                array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'ask' => array(self::BELONGS_TO, 'Ask', 'ask_id'),
            'floorReplies' => array(self::HAS_MANY, 'AskReplyReply', 'be_replied_id'),
            'floorReplielist' => array(self::MANY_MANY, 'AskReply', ' tbl_ask_reply_reply(reply_id, reply_id)'),
            'floorReplyCount' => array(self::STAT, 'AskReplyReply', 'be_replied_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'reply_id' => Yii::t('askReply/model',  'Reply'),
			'ask_id' => Yii::t('askReply/model',  'Ask'),
			'body' =>  Yii::t('askReply/model',  'Body'),
			'user_id' => Yii::t('askReply/model',   'User'),
			'createdOn' => Yii::t('askReply/model',   'Created On'),
			'isFloor' =>  Yii::t('askReply/model',  'Is Floor'),
			'status' =>  Yii::t('askReply/model',  'Status'),
                                                                'verifyCode' => Yii::t('askReply/model', 'Verify Code'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('reply_id',$this->reply_id);
		$criteria->compare('ask_id',$this->ask_id);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('createdOn',$this->createdOn,true);
		$criteria->compare('isFloor',$this->isFloor);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return AskReply the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
        * 保存数据前的数据处理
        * 
        * @ author : Huang Yuetian
        * @date : 07/24/2014
        */
	public function beforeSave()
	{
		if ($this->isNewRecord)
		{
			$this->user_id = Yii::app()->user->id;
                                                                $this->status = 1;
		}
              
                                           return parent::beforeSave();
	}
}
